<template>
  <div class="processing-info-card">
    <div class="card-content">
      <!-- 处理人员卡片 -->
      <div class="info-card" :class="{ 'filled': formData.handler }" @click="openHandlerDialog">
        <div class="fill-indicator"></div>
        <div class="card-label">
          处理人员
          <el-tooltip content="指定负责处理此故障的维修人员。系统会根据故障类型和人员专长推荐合适的处理人员。" placement="top">
            <i class="el-icon-question help-icon"></i>
          </el-tooltip>
        </div>
        <div class="card-value" v-if="formData.handler">
          {{ formData.handler }}
        </div>
        <div class="card-placeholder" v-else>
          点击分配处理人员
        </div>
      </div>

      <!-- 预计时间卡片 -->
      <div class="info-card" :class="{ 'filled': formData.estimatedTime }" @click="openTimeDialog">
        <div class="fill-indicator"></div>
        <div class="card-label">
          预计完成时间
          <el-tooltip content="设置预计完成维修的时间。系统会根据故障类型和历史数据提供时间估算，可根据实际情况调整。" placement="top">
            <i class="el-icon-question help-icon"></i>
          </el-tooltip>
        </div>
        <div class="card-value" v-if="formData.estimatedTime">
          {{ formatTime(formData.estimatedTime) }}
        </div>
        <div class="card-placeholder" v-else>
          点击设置预计时间
        </div>
      </div>

      <!-- 处理方案卡片 -->
      <div class="info-card full-width" :class="{ 'filled': formData.solution }">
        <div class="fill-indicator"></div>
        <div class="card-label">
          处理方案
          <el-tooltip content="详细的维修处理方案，包括具体操作步骤、所需工具材料、注意事项等。支持Markdown格式。" placement="top">
            <i class="el-icon-question help-icon"></i>
          </el-tooltip>
        </div>
        <div class="card-content-area">
          <MarkdownRenderer
            v-model="formData.solution"
            :editable="true"
            :collapsible="true"
            :maxHeight="80"
            :showLabel="false"
            placeholder="详细的处理方案，支持Markdown语法..."
            :rows="4"
          />
        </div>
      </div>

      <!-- 现场记录卡片 -->
      <div class="info-card full-width" :class="{ 'filled': formData.onSiteObservation }">
        <div class="fill-indicator"></div>
        <div class="card-label">
          现场观察记录
          <el-tooltip content="记录现场实际情况、维修过程、遇到的问题和解决方法等。支持Markdown格式。" placement="top">
            <i class="el-icon-question help-icon"></i>
          </el-tooltip>
        </div>
        <div class="card-content-area">
          <MarkdownRenderer
            v-model="formData.onSiteObservation"
            :editable="true"
            :collapsible="true"
            :maxHeight="80"
            :showLabel="false"
            placeholder="记录现场情况，支持Markdown语法..."
            :rows="4"
          />
        </div>
      </div>
    </div>

    <!-- 处理人员选择弹窗 -->
    <el-dialog
      title="选择处理人员"
      :visible.sync="handlerDialogVisible"
      width="500px"
      :close-on-click-modal="false"
      :before-close="() => closeDialog('handler')">
      <el-select
        v-model="tempHandlerId"
        filterable
        placeholder="请选择处理人员"
        style="width: 100%">
        <el-option
          v-for="user in userList"
          :key="user.value"
          :label="user.label"
          :value="user.value">
        </el-option>
      </el-select>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog('handler')">取消</el-button>
        <el-button type="primary" @click="saveHandler">确定</el-button>
      </div>
    </el-dialog>

    <!-- 时间选择弹窗 -->
    <el-dialog
      title="预计完成时间"
      :visible.sync="timeDialogVisible"
      width="400px"
      :close-on-click-modal="false"
      :before-close="() => closeDialog('time')">
      <el-date-picker
        v-model="tempTime"
        type="datetime"
        placeholder="选择预计完成时间"
        style="width: 100%"
        value-format="yyyy-MM-dd HH:mm:ss"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog('time')">取消</el-button>
        <el-button type="primary" @click="saveTime">确定</el-button>
      </div>
    </el-dialog>

    <!-- 处理方案和现场记录弹窗已改为内联Markdown编辑器 -->
  </div>
</template>

<script>
import dialogMixin from '@/mixins/dialogMixin'

export default {
  name: 'ProcessingInfoForm',
  mixins: [dialogMixin],
  props: {
    formData: {
      type: Object,
      required: true
    },
    userList: {
      type: Array,
      default: () => []
    },
    datePickerOptions: {
      type: Object,
      default: () => ({})
    },
    compact: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      handlerDialogVisible: false,
      timeDialogVisible: false,
      tempHandler: '',
      tempHandlerId: null,
      tempTime: '',
    }
  },
  methods: {
    validate() {
      // 简单验证必填字段
      if (!this.formData.handler) {
        this.$message.error('请选择处理人员');
        return Promise.reject();
      }
      return Promise.resolve();
    },

    openHandlerDialog() {
      this.safeOpenDialog(() => {
        this.tempHandlerId = this.formData.handlerId || null;
        this.handlerDialogVisible = true;
      });
    },

    openTimeDialog() {
      this.safeOpenDialog(() => {
        this.tempTime = this.formData.estimatedTime || '';
        this.timeDialogVisible = true;
      });
    },

    // openSolutionDialog 和 openObservationDialog 已移除，改为内联Markdown编辑器

    closeDialog(type) {
      this.safeCloseDialog(() => {
        switch(type) {
          case 'handler':
            this.handlerDialogVisible = false;
            break;
          case 'time':
            this.timeDialogVisible = false;
            break;
        }
      });
    },

    saveHandler() {
      const selectedUser = this.userList.find(user => user.value === this.tempHandlerId);
      if (selectedUser) {
        this.formData.handler = selectedUser.label;
        this.formData.handlerId = selectedUser.value;
      }
      this.closeDialog('handler');
    },

    saveTime() {
      this.formData.estimatedTime = this.tempTime;
      this.closeDialog('time');
    },

    // saveSolution 和 saveObservation 已移除，改为内联Markdown编辑器自动保存

    formatTime(time) {
      if (!time) return '';
      return new Date(time).toLocaleString('zh-CN');
    }
  }
}
</script>

<style scoped>
/* 处理信息卡片容器 */
.processing-info-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  transition: all 0.3s ease;
}



/* 卡片内容 */
.card-content {
  padding: 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* 信息卡片 */
.info-card {
  background: #f8fafc;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  padding: 16px 16px 16px 24px;
  cursor: pointer;
  position: relative;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  user-select: none;
}

/* 填写状态指示器 */
.fill-indicator {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  background: #e2e8f0;
  border-radius: 12px 0 0 12px;
  transition: all 0.3s ease;
}

.info-card.filled .fill-indicator {
  background: linear-gradient(135deg, #10b981, #059669);
  box-shadow: 0 0 8px rgba(16, 185, 129, 0.3);
}

.info-card.full-width {
  grid-column: 1 / -1;
}



.card-label {
  font-size: 12px;
  font-weight: 600;
  color: #64748b;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.help-icon {
  font-size: 14px;
  color: #9ca3af;
  cursor: help;
  transition: color 0.2s ease;
}

.help-icon:hover {
  color: #6b7280;
}

.card-value {
  font-size: 14px;
  font-weight: 500;
  color: #1e293b;
  line-height: 1.5;
  flex: 1;
}

.card-placeholder {
  font-size: 14px;
  color: #94a3b8;
  font-style: italic;
  flex: 1;
}

.card-content-area {
  margin-top: 8px;
}



/* 处理人员选项样式 */
.handler-option {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.handler-option small {
  color: #64748b;
  font-size: 12px;
}

/* 弹窗样式 */
.clean-textarea >>> .el-textarea__inner {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.6;
  padding: 12px;
  transition: border-color 0.2s ease;
}

.clean-textarea >>> .el-textarea__inner:focus {
  border-color: #8b5cf6;
  box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
}

/* 弹窗z-index修复 */
.el-dialog__wrapper {
  z-index: 3000 !important;
}

.el-overlay {
  z-index: 2999 !important;
}

/* 日期选择器z-index修复 */
.el-picker-panel {
  z-index: 3001 !important;
}

.el-date-picker__time-header {
  z-index: 3001 !important;
}

.el-time-panel {
  z-index: 3002 !important;
}

/* 下拉选择器z-index修复 */
.el-select-dropdown {
  z-index: 3001 !important;
}

.el-popper {
  z-index: 3001 !important;
}

.dialog-footer {
  text-align: right;
}

.dialog-footer .el-button {
  border-radius: 8px;
  font-weight: 500;
  padding: 8px 16px;
}

.dialog-footer .el-button--primary {
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  border: none;
  box-shadow: 0 2px 4px rgba(139, 92, 246, 0.3);
}



/* 响应式设计 */
@media (max-width: 768px) {
  .card-content {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}
</style>

<style scoped>
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.section-header h2 {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin: 0;
}

.handler-option {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.handler-option small {
  color: #909399;
  font-size: 12px;
}
</style> 